﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TableCodeGenerator.cs" company="Data Ductus Malmö">
//   (c) 2010 - 2012 Data Ductus Malmö AB
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace DataDuctus.Snmp.Persistor.Core.CodeGen
{
  using System.Collections.Generic;
  using System.IO;
  using System.Linq;

  using DataDuctus.Snmp.Persistor.Core.Schema;

  using Lextm.SharpSnmpLib;

  /// <summary>
  /// The table code generator.
  /// </summary>
  public class TableCodeGenerator : ICodeGenerator
  {
    #region Public Properties

    /// <summary>
    /// Gets the type.
    /// </summary>
    public CodeGeneratorTypes Type
    {
      get
      {
        return CodeGeneratorTypes.Table;
      }
    }

    #endregion

    #region Public Methods and Operators

    /// <summary>
    /// The generate.
    /// </summary>
    /// <param name="item">
    /// The item.
    /// </param>
    /// <param name="profile">
    /// The profile.
    /// </param>
    /// <param name="codeStream">
    /// The code stream.
    /// </param>
    /// <param name="config">
    /// The config.
    /// </param>
    public void Generate(
      ISchemaItem item, ICodeGeneratorProfile profile, TextWriter codeStream, CodeGeneratorConfiuguration config)
    {
      IEnumerable<ISchemaItem> columns = from cols in item.Children.First().Children
                                         where cols.Type == DefinitionType.Column
                                         select cols;

      foreach (ISchemaItem column in columns)
      {
        var generator = (IColumnarCodeGenerator)profile.CodeGeneratorFactory.Create(CodeGeneratorTypes.Columnar, column);
        generator.Generate(column, item.Children.First(), item, profile, codeStream, config);
      }
    }

    #endregion
  }
}